package com.chuanyang.driverh.mainmy.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;
import com.chuanyang.driverh.R;

import java.util.List;

/**
 * 地图选点的adapter
 * @author joyskim
 *
 */
public class ListMapAddressAdapter extends BaseAdapter{
	
	private Context context;
	private List<PoiItem> addrs;
	
	
	public ListMapAddressAdapter(Context context, List<PoiItem> addrs) {
		this.context = context;
		this.addrs = addrs;
	}
	
	@Override
	public int getCount() {
		return addrs.size();
	}
	
	public void clear(){
		addrs.clear();
		notifyDataSetChanged();
	}

	public void add(List<PoiItem> data){
		addrs.addAll(data);  
		notifyDataSetChanged();
	}

	@Override
	public Object getItem(int position) {
		return addrs.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder h=null;
		if(convertView==null){
			convertView=View.inflate(context, R.layout.my_item_select_addr_as_input, null);
			h=new ViewHolder();
			h.tvBulding=((TextView)convertView.findViewById(R.id.tv_addr_name));
			h.tvAddress=((TextView)convertView.findViewById(R.id.tv_addr_addr));
			convertView.setTag(h);
		}else{
			h=(ViewHolder) convertView.getTag();
		}
		
		PoiItem one = addrs.get(position);
		h.tvBulding.setText(one.getTitle());
		if(TextUtils.isEmpty(one.getSnippet())){
			h.tvAddress.setText(one.getTitle());
		}else{
			h.tvAddress.setText(one.getSnippet());
		}
		return convertView;
	}
	
	class ViewHolder{
		TextView tvBulding;
		TextView tvAddress;
	}
	
}
